<!-- subpkg_archive/list/index.vue -->
<script lang="ts" setup>
  import { delPatientItemAPI, getPatientListAPI } from '@/api/archive'
  import { PAGE_PATH } from '@/constants'
  import { useConsultStore } from '@/store/consult'
  import type { PatientItem } from '@/types/archive'
  import { onShow } from '@dcloudio/uni-app'
  import { ref } from 'vue'
  const store = useConsultStore()
  const patientList = ref<PatientItem[]>([{} as PatientItem])
  const props = defineProps<{ type: string }>()
  //导航栏标题
  if (props.type === 'consult') {
    uni.setNavigationBarTitle({ title: '选择患者信息' })
  }

  onShow(() => {
    getPatientList()
  })
  const getPatientList = async () => {
    patientList.value = await getPatientListAPI()
    if (props.type === 'consult') {
      // 此时说明在极速问诊的场景下 并且当前没有被选中的患者 才需要把默认的患者当成选中患者
      if (!store.illnessInfo.patientId) {
        // 此时说明没有选中患者
        // 把默认患者设置成选中患者
        store.illnessInfo.patientId =
          patientList.value.find((item) => item.defaultFlag === 1)?.id || ''
      }
    }
  }
  const toUrl = ref<string>()
  const swipeOptions = ref([
    {
      text: '删除',
      style: {
        backgroundColor: '#dd524d',
        color: '',
        fontSize: '',
      },
    },
  ])
  const onClick = async (id: string) => {
    await delPatientItemAPI(id)
    uni.showToast({
      title: '删除成功',
      icon: 'none',
    })
    getPatientList()
  }
  const toEditItem = async (id: string) => {
    toUrl.value = `/subpkg_archive/form/index?id=${id}`
  }

  const selectPatient = (id: string) => {
    store.illnessInfo.patientId = id // 记录当前患者的id
  }
  // 点击下一步
  const goTOPay = () => {
    if (!store.illnessInfo.patientId) {
      uni.showToast({ title: '请先选择患者', icon: 'none' })
      return
    }
    uni.navigateTo({
      url: PAGE_PATH.CONSULT_PAY_PAGE,
    })
  }
</script>

<template>
  <scroll-page>
    <view class="archive-page">
      <view class="archive-tips">最多可添加6人</view>
      <uni-swipe-action>
        <uni-swipe-action-item
          v-for="item in patientList"
          :key="item.id"
          :right-options="swipeOptions"
          @click="onClick(item.id)"
        >
          <view
            @click="selectPatient(item.id)"
            class="archive-card"
            :class="{ active: store.illnessInfo.patientId === item.id }"
          >
            <view class="archive-info">
              <text class="name">{{ item.name }}</text>
              <text class="id-card">{{
                item.idCard.replace(/^(.{6}).+(.{4})$/, '$1********$2')
              }}</text>
              <text class="default" v-if="!!item.defaultFlag">{{
                item.defaultFlag === 0 ? '' : '默认'
              }}</text>
            </view>
            <view class="archive-info">
              <text class="gender">{{ item.gender === 1 ? '男' : '女' }}</text>
              <text class="age">{{ item.age }}岁</text>
            </view>
            <navigator hover-class="none" class="edit-link" :url="toUrl">
              <uni-icons
                type="icon-edit"
                size="20"
                color="#16C2A3"
                custom-prefix="iconfont"
                @click="toEditItem(item.id)"
              />
            </navigator>
          </view>
        </uni-swipe-action-item>

        <!-- <uni-swipe-action-item :right-options="swipeOptions">
          <view class="archive-card">
            <view class="archive-info">
              <text class="name">李富贵</text>
              <text class="id-card">321***********6164</text>
            </view>
            <view class="archive-info">
              <text class="gender">男</text>
              <text class="age">32岁</text>
            </view>
            <navigator hover-class="none" class="edit-link" url="/subpkg_archive/form/index">
              <uni-icons type="icon-edit" size="20" color="#16C2A3" custom-prefix="iconfont" />
            </navigator>
          </view>
        </uni-swipe-action-item> -->
      </uni-swipe-action>

      <!-- 添加按钮 -->
      <view v-if="true" class="archive-card">
        <navigator class="add-link" hover-class="none" url="/subpkg_archive/form/index">
          <uni-icons color="#16C2A3" size="24" type="plusempty" />
          <text class="label">添加患者</text>
        </navigator>
      </view>
      <view class="next-step" v-if="props.type === 'consult'">
        <button @click="goTOPay" class="uni-button">下一步</button>
      </view>
    </view>
  </scroll-page>
</template>

<style lang="scss">
  @import './index.scss';
</style>
